package manage.handler;

import manage.entity.StudentDo;
import manage.service.StudentService;
import manage.service.impl.StudentServiceImpl;
import manage.student.MainView;
import manage.student.view.AddStudentView;
import manage.student.view.UpdateStudentView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateStudentViewHandler implements ActionListener {
    private UpdateStudentView updateStudentView;
    private MainView mainView;
    public UpdateStudentViewHandler(UpdateStudentView updateStudentView, MainView mainView){
        this.updateStudentView=updateStudentView;
        this.mainView=mainView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("修改".equals(text)){
            StudentService studentService=new StudentServiceImpl();
            StudentDo studentDo=updateStudentView.buildUpdateStudent();
            boolean updateResult=studentService.update(studentDo);
            if(updateResult){
                //更新表格数据
                mainView.reloadTable();
                updateStudentView.dispose();
                JOptionPane.showMessageDialog(updateStudentView,"修改成功");
            }else{
                JOptionPane.showMessageDialog(updateStudentView,"修改失败");
            }
        }
    }
}
